<template>
  <a-modal
    :title="'备注'"
    width="50%"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="false">
      <a-form :form="form">
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea :rows="6" placeholder="请输入备注" v-decorator="['cprkRemark']" allowClear :maxLength="500"></a-textarea>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { editRemarkApi } from '@/api/modular/main/emsorder/emsOrderManage'
  export default {
    data () {
      return {
				dialogType:'',
				orderId:'',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
      }
    },
    methods: {
      // 初始化方法
      init (dialogType, record) {
				console.log('record', record)
				this.dialogType = dialogType
				this.orderId = record.orderId
        this.visible = true
				this.$nextTick(()=>{
					// 编辑和详情需要回显
					if(dialogType!='add'){
						this.form.setFieldsValue(
							{
								cprkRemark: record.cprkRemark
							}
						)
					}
				})
      },
      handleSubmit () {
				let text = this.dialogType == 'edit' ? '编辑' : '新增'
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
						let query = {
							orderId: this.orderId,
							cprkRemark: values.cprkRemark
						}
            editRemarkApi(query).then((res) => {
              if (res.success) {
                this.$message.success(`${text}成功`)
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error(`${text}失败` + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
